package OptimizeDAO;


import bean.Customers;
import org.junit.jupiter.api.Test;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.Date;
import java.util.List;

/**
 * 用户生成测试类
 */
class CustomerDAOImplTest {

    private CustomerDAOImpl customerDAO=new CustomerDAOImpl();
    @Test
    void insertCustomer() {
        Connection connection=null;
        try {
             connection= JDBCUtils.getConnection();
            Customers customers=new Customers(null,"大熊","daxiong@163.com",new Date(43354646435L));
            boolean b = customerDAO.insertCustomer(connection, customers);
            if(b){
                System.out.println("插入成功");
            }else {
                System.out.println("插入失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(connection,null);
        }
    }

    @Test
    void deleteById() {
        Connection connection=null;
        try {
            connection= JDBCUtils.getConnection();
            boolean b = customerDAO.deleteById(connection, 18);
            if(b){
                System.out.println("删除成功");
            }else {
                System.out.println("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(connection,null);
        }
    }

    @Test
    void updateById() {
        Connection connection=null;
        try {
            connection= JDBCUtils.getConnection();
            Customers customers=new Customers(18,"光头强","guangtouqiang@qq.com",new Date(43354646435L));
            boolean b = customerDAO.updateById(connection, customers);
            if(b){
                System.out.println("更新成功");
            }else {
                System.out.println("更新失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(connection,null);
        }
    }

    @Test
    void getCustomerById() {
        Connection connection=null;
        try {
            connection= JDBCUtils.getConnection();
            Customers customers = customerDAO.getCustomerById(connection, 19);
            if(customers!=null){
                System.out.println(customers);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(connection,null);
        }
    }

    @Test
    void getAllCustomer() {
        Connection connection=null;
        try {
            connection= JDBCUtils.getConnection();

            List<Customers> allCustomer = customerDAO.getAllCustomer(connection);
            allCustomer.forEach(System.out::println);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(connection,null);
        }
    }

    @Test
    void getCount() {
        Connection connection=null;
        try {
            connection= JDBCUtils.getConnection();
            long b = customerDAO.getCount(connection);
            System.out.println("表中的记录数为"+b);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(connection,null);
        }
    }

    @Test
    void getMaxBirth() {
        Connection connection=null;
        try {
            connection= JDBCUtils.getConnection();
            Date maxBirth= (Date) customerDAO.getMaxBirth(connection);
            System.out.println("最大的生日为"+maxBirth);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.closeResource(connection,null);
        }
    }
}